package data.controller
{
	import flash.events.EventDispatcher;
	import base.net.NetFactory;
	import base.net.socket.events.SocketConnectionEvent;
	import base.net.socket.packet.SocketPacketBase;

	public class DataReceiveController extends EventDispatcher
	{
		public function DataReceiveController()
		{
			NetFactory.Instance.socketController.addEventListener(SocketConnectionEvent.RECEIVABLE_PACKET, __onReceivablePacket);
		}
		
		private static var _instance:DataReceiveController;
		public static function get Instance() : DataReceiveController
		{
			if(_instance == null)
			{
				_instance = new DataReceiveController();
			}
			return _instance;
		}
		
		/**
		 * Socket数据返回
		 * @param object 数据
		 */
		private function __onReceivablePacket(evt:SocketConnectionEvent):void
		{
			var socketPacketBase:SocketPacketBase=evt.data as SocketPacketBase;
			dispatchEvent(new DataReceiveEvent(socketPacketBase.packetCode.toString(), socketPacketBase));
		}
		
		/**
		 *Socket数据返回侦听 
		 * @param packCode 协议号
		 * @param listener 处理事件的侦听器函数
		 * @param useCapture 确定侦听器是运行于捕获阶段还是运行于目标和冒泡阶段
		 * @param priority 事件侦听器的优先级
		 * @param useWeakReference 确定对侦听器的引用是强引用，还是弱引用
		 */		
		public function addDataReceiveListener(packCode:int, listener:Function, useCapture:Boolean=false, priority:int=0, useWeakReference:Boolean=false):void
		{
			super.addEventListener(packCode.toString(), listener, useCapture, priority, useWeakReference);
		}
	}
}